package com.ttbj.websocket;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

//webSocket的配置
@Configuration
@EnableWebMvc
@EnableWebSocket
public class WebSocketConfig extends WebMvcConfigurerAdapter implements WebSocketConfigurer{

	@Override
	public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
		System.out.println("webSocketConfig....");
		//允许连接的域，只能以http或者https开头，或者使用*号允许全部域名访问
		String[] allowsOrigins = {"*"};	//"http://localhost:8081", "https://localhost:8081"
		//WebSocket通道
		registry.addHandler(messageHandler(), "/push_msg.htm")
				.setAllowedOrigins(allowsOrigins).addInterceptors(messageIterator());
		
		registry.addHandler(messageHandler(), "/sockjs/push_msg.htm")
				.setAllowedOrigins(allowsOrigins).addInterceptors(messageIterator());
	}
	
	@Bean
	public PushSalMessageHandler messageHandler() {
		return new PushSalMessageHandler();
	}
	
	@Bean
	public PushSalMessageIterator messageIterator() {
		return new PushSalMessageIterator();
	}
}
